package de.openlaboratory.elib.controller.servlets;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.openlaboratory.elib.controller.ElibService;
import de.openlaboratory.elib.core.ComponentRegistry;
import de.openlaboratory.elib.core.LogManager;
import de.openlaboratory.elib.model.library.Rendition;
import de.openlaboratory.elib.utilities.OpenLabUtilities;
import de.openlaboratory.elib.utilities.Operation;

public class FileImportServlet extends GenericDataServlet
{
	private static final long serialVersionUID = 1183063826482544696L;
	
	private static final String SERVLET_DESCRIPTION = "Rendition Functionalities";
	

	private static final String TEMPLATE = "/jsf/defaultDesktopTemplate.xhtml";
	private static final String NAVIGATION_MENU = "/jsf/defaultMenu.xhtml";
	private static final String EDIT_OBJECT = "/jsf/fileNavigation.xhtml";

	Rendition newDataObject;
	
	@Override
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		messages = new HashMap<String, String>();
		request.setAttribute("messages", messages);

		String action = (String) request.getParameter("elib_action");
		String elibNamespace = (String) request.getParameter("elib_ns");
		String elibRepository = (String) request.getParameter("elib_repository");
		
		Operation op = Operation.fromString(action);
		
		newDataObject = (Rendition) getDataObjectFromServletRequest(newDataObject, request);

		String page = null;

		switch (op)
		{
			case CREATE:
				page = createDataObject(request, newDataObject);
				break;
			case UPDATE:
				page = updateDataObject(request, newDataObject);
				break;
			case DELETE:
				deleteDataObject(newDataObject.getId());
			default:
				request.setAttribute("param", newDataObject);
				request.setAttribute("elib_action", Operation.CREATE.getText());
				page = EDIT_OBJECT;
				break;
		}
		
		configuration.setMainPage(page);

		configuration.setStyleSheets(null);
		configuration.setTitle(SERVLET_DESCRIPTION);
		configuration.setMenu(NAVIGATION_MENU);
		configuration.setMenuItems(page);

//		List<Library> libraries = getLibraryList();
//		request.setAttribute("libraries", libraries);

//		List<Author> authors = getAuthorList();
//		request.setAttribute("authors", authors);

		request.setAttribute("configuration", configuration);
		
		request.setAttribute("template", TEMPLATE);
		
		RequestDispatcher view = request.getRequestDispatcher(EDIT_OBJECT);
		view.forward(request, response);
	}

	private <T> Object getDataObjectFromServletRequest(T o, HttpServletRequest request)
	{
		Long id = OpenLabUtilities.stringToLong(request.getParameter("id"));
		String name = request.getParameter("name");
		Integer order = OpenLabUtilities.stringToInt(request.getParameter("order"));
		Long publicationId = OpenLabUtilities.stringToLong(request.getParameter("pub_id"));

		if ((name == null || name.trim().isEmpty()))
		{
			messages.put("name", "Please select a file");
		}
		
		if (publicationId <= 0)
		{
			messages.put("name", "Please select a publication");
		}
		
		return new Rendition(id, name, order, publicationId);
	}
	
	private String createDataObject(HttpServletRequest request, Rendition newFile)
	{
			ElibService service = ComponentRegistry.lookupService(Rendition.class, Operation.CREATE);
			Rendition rendition = (Rendition) service.execute(Operation.CREATE, newFile).get(0);

			messages.put("success", String.format("Success: Rendition [%s: %s] saved.", rendition.getName(), rendition.getPos()));

			return EDIT_OBJECT;
	}

	private String updateDataObject(HttpServletRequest request, Rendition newRendition)
	{
		if (newRendition != null && newRendition.getId() > 0)
		{
			Rendition rendition = null;
			
			if (messages.isEmpty())
			{
				ElibService service = ComponentRegistry.lookupService(Rendition.class, Operation.UPDATE);
				rendition = (Rendition) service.execute(Operation.UPDATE, newRendition).get(0);
				
				LogManager.logMessage("Rendition updated: " + newRendition.getName());
			}
		}
		
		request.setAttribute("elib_action", Operation.UPDATE.toString());

		return EDIT_OBJECT;
	}

	private void deleteDataObject(Long id)
	{
		ElibService service = ComponentRegistry.lookupService(Rendition.class, Operation.DELETE);
		service.execute(Operation.DELETE, id);
	}
	
	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo()
	{
		return SERVLET_DESCRIPTION;
	}
}
